//
//  CCYSubscribeTableViewController.m
//  模拟-百思不得姐
//
//  Created by 程春玉 on 16/8/9.
//  Copyright © 2016年 colin. All rights reserved.
//

#import "CCYSubscribeTableViewController.h"
#import "CCYTitleView.h"
#import "AFNetworking.h"
#import "CCYMeSubscribModel.h"
#import "CCYMeNewTableViewCell.h"

@interface CCYSubscribeTableViewController ()
@property (nonatomic, copy) NSString *urlStr;
@property (nonatomic, strong) NSMutableArray *subArr;
@property (nonatomic, strong) CCYTitleView *titleView;
@end

@implementation CCYSubscribeTableViewController

- (NSMutableArray *)subArr
{
    if (!_subArr) {
        _subArr = [[NSMutableArray alloc] init];
    }
    return _subArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.urlStr = @"http://api.budejie.com/api/api_open.php";
    [self.tableView registerNib:[UINib nibWithNibName:@"CCYMeNewTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 10;
    [self createHeadView];
    [self analyCellData];
}

// 创建头部视图
- (void)createHeadView
{
    self.titleView = [CCYTitleView viewDidLoad];
    _titleView.height = 150;
    self.tableView.tableHeaderView = _titleView;
    self.tableView.tableHeaderView.height = 150;
    [self analyHeadData];
}
// 解析头部视图数据
- (void)analyHeadData
{
    NSDictionary *dic = @{@"a" : @"theme_info", @"c" : @"topic", @"theme_id" : _theme_id};
    [[AFHTTPSessionManager manager] GET:_urlStr parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.navigationItem.title = responseObject[@"info"][@"theme_name"];
        [((CCYTitleView *)self.tableView.tableHeaderView) setMessageWithDic:responseObject[@"info"]];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        XMGLog(@"解析title错误");
    }];
}

// 解析cell数据
- (void)analyCellData
{
    NSString *str = [NSString stringWithFormat:@"http://s.budejie.com/topic/tag-topic/%@/new/bs0315-iphone-4.3/0-20.json", _theme_id];
    [[AFHTTPSessionManager manager] GET:str parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        for (NSDictionary *dic in responseObject[@"list"]) {
            CCYMeSubscribModel *model = [[CCYMeSubscribModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.subArr addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        XMGLog(@"解析cell错误");
    }];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.subArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CCYMeNewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    CCYMeSubscribModel *model = _subArr[indexPath.row];
    if ([model.type isEqualToString:@"video"]) {
        [cell setVideoMessageWithModel:model];
    } else {
        [cell setMessageWithModel:model];
    }
    return cell;
}





@end
